import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

public class XLSXService {
    public static void createReport(List<Report> reports,
                                    String pathToSaveDirectory,
                                    String dayNumber,
                                    String monthNumber,
                                    String yearNumber) throws IOException {
        String fileName = monthNumber + "-" + yearNumber + ".xlsx";
        String sheetName = dayNumber + "." + monthNumber + "." + yearNumber;

        XSSFWorkbook workbook;
        File file = new File(fileName);
        if (file.exists()) {
            FileInputStream in = new FileInputStream(file);
            workbook = new XSSFWorkbook(in);
        } else {
            workbook = new XSSFWorkbook();
        }

        if (workbook.getSheetIndex(sheetName) != -1) {
            workbook.removeSheetAt(workbook.getSheetIndex(sheetName));
        }
        XSSFSheet sheet = workbook.createSheet(sheetName);

        Map<String, Object[]> dataForSheet = new TreeMap<String, Object[]>();
        SimpleDateFormat year = new SimpleDateFormat("yyyy");
        SimpleDateFormat month = new SimpleDateFormat("MM");
        SimpleDateFormat dd = new SimpleDateFormat("dd");
        SimpleDateFormat time = new SimpleDateFormat("HH:mm");
        dataForSheet.put("1", new Object[]{"Вахтер", "Преподаватель", "Ключ", "Действие", "Время"});
        int i = 1;
        for (Report report : reports) {
            Date reportDate = new Date(report.getDate());
            if (year.format(reportDate).equals(yearNumber)
                    & month.format(reportDate).equals(monthNumber)
                    & dd.format(reportDate).equals(dayNumber)
                    ) {
                i++;
                dataForSheet.put("" + i, new Object[]{
                        report.getNameJanitor(),
                        report.getNamePersonnel(),
                        report.getNameKey(),
                        report.getStatus(),
                        time.format(report.getDate())});
            }
        }

        Set<String> keySet = dataForSheet.keySet();
        int rowNumber = 0;
        for (String key : keySet) {
            Row row = sheet.createRow(rowNumber++);
            Object[] objArr = dataForSheet.get(key);
            int cellNumber = 0;
            for (Object obj : objArr) {
                Cell cell = row.createCell(cellNumber++);
                if (obj instanceof String)
                    cell.setCellValue((String) obj);
                else if (obj instanceof Integer)
                    cell.setCellValue((Integer) obj);
            }
        }
        FileOutputStream out = new FileOutputStream(new File(fileName));
        workbook.write(out);
        out.close();
    }
}
